import Md5 from 'crypto-js/md5.js';
import {cacheSet,cacheGet} from '@/utils/encryption.js'
import Vue from 'vue'

/**
 * 设置appstr
 */
export function setAppstr(data){
	return cacheSet('user_app_str', data);
}


/**
 * 设置appstr
 */
export function getAppstr(){
	return cacheGet('user_app_str');
}


/**
 * 获取接口TOKEN
 */
export function getToken(ts) {
	return Md5(ts.toString()).toString();
}
	
/**
 * 获得平台别名
 */
export function getPlatformName(){
	let platform = null;
	//#ifdef MP-WEIXIN
		platform = 'routine'; 
	//#endif
	
	//#ifdef H5
	let ua = navigator.userAgent.toLowerCase();
	if (ua.match(/MicroMessenger/i) == "micromessenger"){
		platform = 'wechatH5';
	}else{
		platform = 'H5';
	}
	//#endif
	
	//#ifdef APP-PLUS
		platform = 'app';
	//#endif
	
	/**字节跳动小程序*/
	//#ifdef MP-TOUTIAO
		platform = 'touTiao';
	//#endif
	
	/**支付宝小程序*/
	//#ifdef MP-ALIPAY
		platform = 'alipay';
	//#endif
	
	/**QQ小程序*/
	//#ifdef MP-QQ
		platform = 'qq';
	//#endif
	
	/**360小程序*/
	//#ifdef MP-360
		platform = '360';
	//#endif
	
	/**百度小程序*/
	//#ifdef MP-BAIDU
		platform = 'baidu';
	//#endif
	
	return platform;
}




/**
 * 图片转base64
 * @param {*} file 
 */
export async function getFileToBase64(file) {
	var base64Data = null;
	//#ifdef H5
	base64Data = await h5FileToBase64(file.file);
	//#endif

	//#ifdef MP-WEIXIN
	base64Data = await mpFileToBase64(file);
	//#endif
	return base64Data;
}

/**
 * 微信小程序图片转base64
 * @param {Object} file
 */
export async function mpFileToBase64(file) {
	return await new Promise((resolve, reject) => {
		uni.request({
		    url: file.url, //仅为示例，并非真实接口地址。
		    responseType: 'arraybuffer',
		    success: (res) => {
				var type = res.header['Content-Type'] ? res.header['Content-Type'] : 'image/jpeg';
				var data = 'data:'+type+';base64,' + uni.arrayBufferToBase64(res.data);
				resolve(data);
		    },
			error:(err) =>{
				console.log(222222,err);
			}
		});
	});
}

/**
 * h5图片转base64
 * @param {Object} file
 */
export async function h5FileToBase64(file) {console.log(22222)
	return await new Promise((resolve, reject) => {
		var reader = new FileReader();
		reader.readAsDataURL(file);
		reader.onload = function(e) {
			var data = e.target.result;
			resolve(data);
		}
	});
}



export function posterCanvas(arr2, store_name, price, ot_price, successFn){
	let that = this;
	uni.showLoading({
		title: '海报生成中',
		mask: true
	});
	const ctx = uni.createCanvasContext('myCanvas');
	ctx.clearRect(0, 0, 0, 0);
	/**
	 * 只能获取合法域名下的图片信息,本地调试无法获取
	 * 
	 */
	ctx.fillStyle = '#fff';
	ctx.fillRect(0, 0, 750, 1150);
	uni.getImageInfo({
		src: 'https://pic.feisuimg.com/upload/vod/20220411-1/af6a2b3c48a508aeb4f48520393a61c5.webp',
		success: function(res) {
			const WIDTH = res.width;
			const HEIGHT = res.height;
			// ctx.drawImage(arr2[0], 0, 0, WIDTH, 1050);
			ctx.drawImage(arr2[1], 0, 0, WIDTH, WIDTH);
			ctx.save();
			let r = 110;
			let d = r * 2;
			let cx = 480;
			let cy = 790;
			ctx.arc(cx + r, cy + r, r, 0, 2 * Math.PI);
			// ctx.clip();
			ctx.drawImage(arr2[2], cx, cy, d, d);
			ctx.restore();
			const CONTENT_ROW_LENGTH = 20;
			let [contentLeng, contentArray, contentRows] = that.textByteLength(store_name,
				CONTENT_ROW_LENGTH);
			if (contentRows > 2) {
				contentRows = 2;
				let textArray = contentArray.slice(0, 2);
				textArray[textArray.length - 1] += '……';
				contentArray = textArray;
			}
			ctx.setTextAlign('left');
			ctx.setFontSize(36);
			ctx.setFillStyle('#000');
			// let contentHh = 36 * 1.5;
			let contentHh = 36;
			for (let m = 0; m < contentArray.length; m++) {
				if (m) {
					ctx.fillText(contentArray[m], 50, 1000 + contentHh * m + 18, 1100);
				} else {
					ctx.fillText(contentArray[m], 50, 1000 + contentHh * m, 1100);
				}
			}
			ctx.setTextAlign('left')
			ctx.setFontSize(72);
			ctx.setFillStyle('#DA4F2A');
			ctx.fillText('￥' + price, 40, 820 + contentHh);
	
			ctx.setTextAlign('left')
			ctx.setFontSize(36);
			ctx.setFillStyle('#999');
				
			
			ctx.setTextAlign('left')
			ctx.setFontSize(28);
			ctx.setFillStyle('#999');
			ctx.fillText('长按或扫描查看', 490, 1030 + contentHh);
			ctx.draw(true, function() {
				uni.canvasToTempFilePath({
					canvasId: 'myCanvas',
					fileType: 'png',
					destWidth: WIDTH,
					destHeight: HEIGHT,
					success: function(res) {
						uni.hideLoading();
						successFn && successFn(res.tempFilePath);
					}
				})
			});
		},
		fail: function(err) {
			uni.hideLoading();
			that.Tips({
				title: '无法获取图片信息'
			});
		}
	})
}
	
	
	/**
	 * 用户信息分享海报
	 * @param array arr2 海报素材  1背景 0二维码
	 * @param string nickname 昵称
	 * @param string sitename 价格
	 * @param function successFn 回调函数
	 * 
	 * 
	 */
	export function userPosterCanvas(arr2, nickname, sitename, index, w, h, successFn) {
		let that = this;
		const ctx = uni.createCanvasContext('myCanvas' + index);
		ctx.clearRect(0, 0, 0, 0);
		/**
		 * 只能获取合法域名下的图片信息,本地调试无法获取
		 * 
		 */
		uni.getImageInfo({
			src: arr2[1],
			success: function(res) {
				const WIDTH = res.width;
				const HEIGHT = res.height;
				ctx.fillStyle = '#fff';
				ctx.fillRect(0, 0, w, h);
				ctx.drawImage(arr2[1], 0, 0, w, h);
				ctx.setTextAlign('left')
				ctx.setFontSize(12);
				ctx.setFillStyle('#333');
	
				// x:240 y:426
				let codex = 0.1906
				let codey = 0.7746
				let codeSize = 0.21666
				let namex = 0.4283
				let namey = 0.8215
				let markx = 0.4283
				let marky = 0.8685
				ctx.drawImage(arr2[0], w * codex, h * codey, w * codeSize, w * codeSize);
				if (w < 270) {
					ctx.setFontSize(8);
				} else {
					ctx.setFontSize(10);
				}
				ctx.fillText(nickname, w * namex, h * namey);
				if (w < 270) {
					ctx.setFontSize(8);
				} else {
					ctx.setFontSize(10);
				}
				ctx.fillText('邀请您加入' + sitename, w * markx, h * marky);
				ctx.save();
				ctx.draw(true, function() {
					uni.canvasToTempFilePath({
						canvasId: 'myCanvas' + index,
						fileType: 'png',
						quality: 1,
						success: function(res) {
							successFn && successFn(res.tempFilePath);
						}
					})
				});
			},
			fail: function(err) {
				uni.hideLoading();
				that.Tips({
					title: '无法获取图片信息'
				});
			}
		})
	};


export function assembleApi(url,api='User'){
	var str = '';
	var h = Vue.prototype['$http'+api+'Uri'].substring(Vue.prototype['$http'+api+'Uri'].length-1);
	if(h === '/'){
		if(h === url.substring(0,1)) {
			str = Vue.prototype['$http'+api+'Uri'] + url.substring(1,url.length)
		}else{
			str = Vue.prototype['$http'+api+'Uri'] + url;
		}
	}else{
		if(url.substring(0,1) === '/') {
			str = Vue.prototype['$http'+api+'Uri'] + url.substring(1,url.length)
		}else{
			str = Vue.prototype['$http'+api+'Uri'] + '/' + url;
		}
	}
	return str;
}